package com.besall.allbase.view.activity.chipstoollevel4.protocol.Settings;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.cardview.widget.CardView;

import com.bes.sdk.utils.SettingsJsonUtils;

import java.util.List;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.lib.devices.sgsettingentity.setting.PanelModel;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.ui.GsonUtils;
import bb.lanxing.utils.PanelModelToJsonUtil;

public class DeviceAutoLapActivity extends BaseActivity {
    private static final int REQUEST_CODE_TARGET = 2563;

    private TextView timeTitle;
    private TextView timeValue;
    private ImageView timeSelect;
    private TextView distanceTitle;
    private TextView distanceValue;
    private ImageView distanceSelect;
    private TextView caloriesTitle;
    private TextView caloriesValue;
    private ImageView caloriesSelect;
    private TextView noTargetTitle;
    private ImageView noTargetSelect;

    private String device_model = "BC270";
    private String sn = "test";
    private PanelModel PANEL_MODEL;
    private int sportMode;
    private int autolapType;
    private int autolapValue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_target);

        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(v -> onBackPressed());

        TextView title = findViewById(R.id.title);
        title.setText(getIntent().getStringExtra("title"));

        sportMode = getIntent().getIntExtra("sportMode", 0);
        PANEL_MODEL = GsonUtils.Companion.getInstance().fromJson(getIntent().getStringExtra("PANEL_MODEL"), PanelModel.class);
        if (PANEL_MODEL != null) {
            autolapType = PANEL_MODEL.getProfilesList().get(sportMode).getAutolap().get(1);
            autolapValue = PANEL_MODEL.getProfilesList().get(sportMode).getAutolap().get(2);
        }

        CardView time = findViewById(R.id.time);
        time.setOnClickListener(v -> {
            Intent intent = new Intent(this, DeviceTarget1Activity.class);
            intent.putExtra("title", getIntent().getStringExtra("title")
                    + " " + getString(R.string.device_profiles_setting_detail_target_time));
            intent.putExtra("targetType", 0);
            intent.putExtra("targetValue", autolapValue);
            intent.putExtra("isAutoLap", true);
            intent.putExtra("PANEL_MODEL", PanelModelToJsonUtil.toJson(PANEL_MODEL));
            startActivityForResult(intent, REQUEST_CODE_TARGET);
        });
        timeTitle = findViewById(R.id.timeTitle);
        timeTitle.setText(R.string.device_profiles_setting_detail_lap_mode_time);
        timeValue = findViewById(R.id.timeValue);
        timeSelect = findViewById(R.id.timeSelect);

        CardView distance = findViewById(R.id.distance);
        distance.setOnClickListener(v -> {
            Intent intent = new Intent(this, DeviceTarget1Activity.class);
            intent.putExtra("title", getIntent().getStringExtra("title")
                    + " " + getString(R.string.device_profiles_setting_detail_target_distance));
            intent.putExtra("targetType", 1);
            intent.putExtra("targetValue", autolapValue);
            intent.putExtra("isAutoLap", true);
            intent.putExtra("PANEL_MODEL", PanelModelToJsonUtil.toJson(PANEL_MODEL));
            startActivityForResult(intent, REQUEST_CODE_TARGET);
        });
        distanceTitle = findViewById(R.id.distanceTitle);
        distanceTitle.setText(R.string.device_profiles_setting_detail_lap_mode_distance);
        distanceValue = findViewById(R.id.distanceValue);
        distanceSelect = findViewById(R.id.distanceSelect);

        CardView calories = findViewById(R.id.calories);
        calories.setOnClickListener(v -> {
            Intent intent = new Intent(this, DeviceTarget1Activity.class);
            intent.putExtra("title", getIntent().getStringExtra("title")
                    + " " + getString(R.string.device_profiles_setting_detail_target_calories));
            intent.putExtra("targetType", 2);
            intent.putExtra("targetValue", autolapValue);
            intent.putExtra("isAutoLap", true);
            intent.putExtra("PANEL_MODEL", PanelModelToJsonUtil.toJson(PANEL_MODEL));
            startActivityForResult(intent, REQUEST_CODE_TARGET);
        });
        caloriesTitle = findViewById(R.id.caloriesTitle);
        caloriesTitle.setText(R.string.device_profiles_setting_detail_lap_mode_calories);
        caloriesValue = findViewById(R.id.caloriesValue);
        caloriesSelect = findViewById(R.id.caloriesSelect);

        CardView no_target = findViewById(R.id.no_target);
        no_target.setOnClickListener(v -> {
            if (PANEL_MODEL != null) {
                List<Integer> autolap = PANEL_MODEL.getProfilesList().get(sportMode).getAutolap();
                autolap.set(1, 3);
                autolap.set(2, 0);
            }
            String content = PanelModelToJsonUtil.toJson(PANEL_MODEL);
            device_model = SettingsJsonUtils.getDeviceModel(this);
            sn = SettingsJsonUtils.getSn(this);
            CommonUtil.saveDeviceProfile(content, device_model, sn);
            CommonUtil.sendPanelsJsonFile(context, device_model, sn);
            onBackPressed();
        });
        noTargetTitle = findViewById(R.id.noTargetTitle);
        noTargetTitle.setText(R.string.device_profiles_setting_detail_lap_mode_location);
        noTargetSelect = findViewById(R.id.noTargetSelect);

        changeStatus();
    }

    private void changeStatus() {
        timeValue.setText("");
        distanceValue.setText("");
        caloriesValue.setText("");
        timeSelect.setVisibility(View.GONE);
        distanceSelect.setVisibility(View.GONE);
        caloriesSelect.setVisibility(View.GONE);
        noTargetSelect.setVisibility(View.GONE);
        switch (autolapType) {
            case 0:
                timeValue.setText(autolapValue + getString(R.string.device_profiles_setting_detail_target_time_title));
                timeSelect.setVisibility(View.VISIBLE);
                break;
            case 1:
                distanceValue.setText(autolapValue + getString(R.string.device_profiles_setting_detail_target_distance_title));
                distanceSelect.setVisibility(View.VISIBLE);
                break;
            case 2:
                caloriesValue.setText(autolapValue + getString(R.string.device_profiles_setting_detail_target_calories_title));
                caloriesSelect.setVisibility(View.VISIBLE);
                break;
            case 3:
                noTargetSelect.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("PANEL_MODEL", PanelModelToJsonUtil.toJson(PANEL_MODEL));
        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_TARGET) {
            PANEL_MODEL = GsonUtils.Companion.getInstance().fromJson(data.getStringExtra("PANEL_MODEL"), PanelModel.class);
            if (PANEL_MODEL != null) {
                autolapType = PANEL_MODEL.getProfilesList().get(sportMode).getAutolap().get(1);
                autolapValue = PANEL_MODEL.getProfilesList().get(sportMode).getAutolap().get(2);
            }
            changeStatus();
        }
    }
}